package team.yidong.fenda.controller;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import team.yidong.fenda.entity.*;
import team.yidong.fenda.repository.HospitalRepository;
import team.yidong.fenda.service.HospitalService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
public class HospitalController {
    @Autowired
    HospitalService hospitalService;

    @Autowired
    HospitalRepository hospitalRepository;

    @RequiresPermissions("hospital")
    @RequestMapping("/hospital")

    public String hospital(HttpSession session, Model model) {
        Integer hospitalId = (Integer) session.getAttribute("loginHospitalId");
        String sysName = (String) session.getAttribute("loginSysUserName");
        model.addAttribute("sysName", sysName);
        if (hospitalId != 1) {
            JsonConfig jsonConfig = new JsonConfig();
            jsonConfig.setExcludes(new String[]{"hospitalPermissionSet", "hospital", "userSet", "masterList"});
            model.addAttribute("hospitals", JSONArray.fromObject(hospitalService.findHospitalById(hospitalId), jsonConfig));
        } else {
            JSONArray jsonArray = JSONArray.fromObject(hospitalService.findAll());
            jsonArray.remove(0);
            model.addAttribute("hospitals", jsonArray);
            model.addAttribute("userList", hospitalService.findAllSysUser());
        }
        return "hospital";
    }

    @RequestMapping("/hospitalPermission")
    public String hospitalPermission(HttpSession session, Model model) {
        Integer hospitalId = (Integer) session.getAttribute("loginHospitalId");
        if (hospitalId == 1) {
            JSONArray jsonArray = JSONArray.fromObject(hospitalService.findAll());
            jsonArray.remove(0);
            model.addAttribute("hospitals", jsonArray);
            model.addAttribute("userList", hospitalService.findAllHospitalAdmin());
        }
        return "hospitalPermission";
    }

    /**
     * 机构表的属性
     *
     * @return
     */
    @RequestMapping("/findHospital")
    @ResponseBody
    public JSONObject findHospital(HttpSession session, Integer limit, Integer offset, HttpServletRequest request, String hospitalName, Double proportion, String tixianTime) {

        Integer hospitalId = (Integer) session.getAttribute("loginHospitalId");
        JSONObject jb = hospitalService.findHospitalCriteria(hospitalId, hospitalName, proportion, tixianTime, offset, limit);
        return jb;
    }

    @RequestMapping("/addHospital")
    @ResponseBody

    public String addHospital(@RequestParam String hospitalName, @RequestParam Double proportion, @RequestParam String userName, @RequestParam String password, @RequestParam String departmentName, @RequestParam String tixianTime, String jgxxgl, String tp, String bmgl, String jgflgl, String jgjjgl, String zjxxgl, String zjdtmx, String yyslgl, String xxgl, String lscx, String wtgl, String hmd, String dd, String yhhhgl, String ztjz) {

        return hospitalService.saveHospital(hospitalName, proportion, userName, password, departmentName, tixianTime, jgxxgl, tp, bmgl, jgflgl, jgjjgl, zjxxgl, zjdtmx, yyslgl, xxgl, lscx, wtgl, hmd, dd, yhhhgl, ztjz);

    }

    /*    @RequestMapping("/deleteHospital")
        @ResponseBody
        public String deleteHospital(@RequestParam String jsonData) {
            JSONArray jsonArray = JSONArray.fromObject(jsonData);
            Map classMap = new HashMap();
            classMap.put("hospitalPermissionSet", HospitalPermission.class);
            classMap.put("hospitalImgSet", HospitalImg.class);
            classMap.put("hospitalFieldList", HospitalField.class);
            classMap.put("userSet", User.class);
            classMap.put("masterList", Master.class);
            Hospital[] hospitals = (Hospital[]) JSONArray.toArray(jsonArray, Hospital.class, classMap);
            List<Hospital> list = Arrays.asList(hospitals);
            for (Hospital hospital : list) {
                System.out.println(hospital.toString());
            }
            hospitalService.deleteHospital(list);
            System.out.println("shanchucheng");
            return "删除成功";
        }*/

    @RequestMapping("/updateHospital")
    @ResponseBody
    public String updateHospital(@RequestParam String jsonData) {
        JSONObject jsonObject = JSONObject.fromObject(jsonData);
        Map classMap = new HashMap();

        classMap.put("hospitalImgSet", HospitalImg.class);
        classMap.put("hospitalFieldList", HospitalField.class);
        classMap.put("userSet", User.class);
        classMap.put("masterList", Master.class);
        Hospital hospital = (Hospital) JSONObject.toBean(jsonObject, Hospital.class, classMap);

        hospitalRepository.save(hospital);
        return "修改成功";
    }

    @RequestMapping("/updateProportion")
    @ResponseBody
    public void updateProportion(@RequestParam Integer hospitalId, @RequestParam Integer tixianTime) {
        hospitalService.updateProportion(hospitalId, tixianTime);
    }

    @RequestMapping("/findHospitalNameById")
    @ResponseBody
    public String findHospitalNameById(Integer hospitalId) {
        return hospitalService.findHospitalNameById(hospitalId);
    }

    @RequestMapping("/findSysUserPermission")
    @ResponseBody
    public JSONArray findSysUserPermission(String hospitalName,String sysUserName) {

        return hospitalService.findAllSysUserPermission(hospitalName, sysUserName);
    }

    @RequestMapping("/findUserRestPermission")
    @ResponseBody
    public JSONArray findUserRestPermission(@RequestParam Integer userId) {
        return hospitalService.findUserRestPermission(userId);
    }

    @RequestMapping("/addSysUserPermission")
    @ResponseBody
    public void addSysUserPermission(@RequestParam Integer sysUserId, @RequestParam Integer permissionId) {
        hospitalService.addSysUserPermission(sysUserId, permissionId);
    }

    @RequestMapping("/deleteSysUserPermission")
    @ResponseBody
    public void deleteSysUserPermissioo(@RequestParam Integer roleId, @RequestParam Integer permissionId) {
        hospitalService.deleteSysUserPermission(roleId, permissionId);
    }
}
